package annotation.immutable;

public class ImmutableJ {

	private Obj obj;
	private final Obj2 finalObj;
	private Obj3 diffensiveObj;

	class Obj {
		private String hoge;
		public Obj(String str) {
			this.hoge = str;
		}
		public void print() {
			System.out.println(hoge);
		}
	}
	class Obj2 {
		private final String hoge;
		public Obj2(String str) {
			this.hoge = str;
		}
		public void print() {
			System.out.println(hoge);
		}
	}

	class Obj3 implements Cloneable {
		private String hoge;
		public Obj3(String str) {
			this.hoge = str;
		}
		public void print() {
			System.out.println(hoge);
		}

		@Override
		protected Object clone() throws CloneNotSupportedException {
			return super.clone();
		}
	}

	public ImmutableJ() {
		obj = new Obj("normal");
		finalObj = new Obj2("final");
		diffensiveObj = new Obj3("diffensive");
	}

	public Obj getObj() {
		return obj;
	}

	public Obj2 getFinalObj() {
		return finalObj;
	}

	public Obj3 getDiffensiveObj() throws Exception {
		return (Obj3) diffensiveObj.clone();
	}

	public void test() {

	}

	public static void main(String[] args) throws Exception {

		ImmutableJ im = new ImmutableJ();
		im.getObj().hoge = "Fuck!";
		im.getObj().print();

		// コンパイルエラー
//		im.getFinalObj().hoge = "Fuck!";
//		im.getFinalObj().print();

		// クローン
		im.getDiffensiveObj().hoge = "Fuck!";
		im.getDiffensiveObj().print();
	}
}
